package com.jack.screenprojectionpushsimple;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import androidx.core.app.NotificationCompat;

import org.jetbrains.annotations.NotNull;

/**
 * @创建者 Jack
 * @创建时间 2024/1/13 13:25
 * @描述 参考 from https://cloud.tencent.com/developer/article/1914827?from=15425
 */
public class MediaService extends Service {
    private final String NOTIFICATION_CHANNEL_ID   = "com.tencent.trtc.apiexample.MediaService";
    private final String NOTIFICATION_CHANNEL_NAME = "com.tencent.trtc.apiexample.channel_name";
    private final String NOTIFICATION_CHANNEL_DESC = "com.tencent.trtc.apiexample.channel_desc";

    private MediaProjectionManager mMediaProjectionManager;

    public MediaService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int resultCode = intent.getIntExtra("code", -1);
        Intent data = intent.getParcelableExtra("data");

        Messenger messenger = (Messenger) intent.getParcelableExtra("messenger");
        if (data != null) {
            MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
            //通过广播的形式将 mediaProjection 传递给activity
//            Intent broadIntent = new Intent("cn.jack.projection");
//            broadIntent.putExtra("mediaProjection",mediaProjection);
//            sendBroadcast(broadIntent);
            if (messenger != null) {
                try {
                    messenger.send(Message.obtain(null, 23, mediaProjection));
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    public void startNotification() {
        //https://blog.csdn.net/juer2017/article/details/133946585
        //限制一下不跳转页面
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //Call Start foreground with notification
            Intent notificationIntent = new Intent(this, MediaService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground))
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("开启了服务")
                    .setContentText("正在使用屏幕录制")
                    //开启点按通知后自动移除通知
                    //            .setAutoCancel(true);
                    .setContentIntent(pendingIntent);
            Notification notification = notificationBuilder.build();
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(NOTIFICATION_CHANNEL_DESC);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
            startForeground(1, notification); //必须使用此方法显示通知，不能使用notificationManager.notify，否则还是会报上面的错误
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MsgBinder();
    }

    public void setMediaProjectionManager(@NotNull MediaProjectionManager mediaProjectionManager) {
        mMediaProjectionManager = mediaProjectionManager;
    }

    public class MsgBinder extends Binder {
        /**
         * 获取当前Service的实例
         *
         * @return
         */
        public MediaService getService() {
            return MediaService.this;
        }
    }
}
